package com.nkzy.controller;
import cn.hutool.core.bean.BeanUtil;
import com.nkzy.domain.dto.UserFormDTO;
import com.nkzy.domain.pojo.User;
import com.nkzy.domain.vo.UserVO;
import com.nkzy.service.UserService;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Tag(name = "用户管理接口")
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping
    @Operation(summary = "新增用户")
    public User saveUser(@RequestBody UserFormDTO userFormDTO){
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        userService.save(user);
        return User.success();
    }
    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户")
    public User deleteUser(@ApiParam(value = "用户id") @PathVariable("id") Long
                                     userId){
        userService.removeById(userId);
        return User.success();
    }
    @GetMapping("/{id}")
    @Operation(summary = "查询用户")
    public User queryUserById(@ApiParam(value = "用户id") @PathVariable("id")
                                Long userId){
        User user = userService.getById(userId);
        UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
        return User.success();
    }
    @GetMapping
    @Operation(summary = "根据id集合查询用户")
    public User queryUserByIds(@ApiParam(value = "用户id集合")
                                 @RequestParam("ids") List<Long> ids){
        List<User> users = userService.listByIds(ids);
        List<UserVO> userVOS = BeanUtil.copyToList(users, UserVO.class);
        return User.success();
    }
    @GetMapping("/all")
    @Operation(summary = "查询所有用户")
    public User queryAllUser(){
        List<User> users = userService.list();
        List<UserVO> allUsers = BeanUtil.copyToList(users, UserVO.class);
        return User.success();

    }
}